package com.lsw.jgyf.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsw.Base.AdapterRefresh;
import com.lsw.jgyf.R;
import com.lsw.jgyf.adapter.CalculationPriceAdapter;
import com.lsw.jgyf.adapter.FujiaxianAdapter;
import com.lsw.jgyf.adapter.PopupWindowSortAdapter;
import com.lsw.jgyf.adapter.PurchaseModeAdapter;
import com.lsw.jgyf.model.CalculationPriceModel;
import com.lsw.jgyf.model.ContractPhoneModel;
import com.lsw.jgyf.model.FenqichanpingModel;
import com.lsw.jgyf.model.FenqigongsiModel;
import com.lsw.jgyf.model.FujiaxianModel;
import com.lsw.jgyf.model.MoreOptionsModel;
import com.lsw.jgyf.model.PopupWindowSortModel;
import com.lsw.jgyf.ui.base.activity.TitleBarActivity;
import com.lsw.util.DensityUtil;
import com.lsw.util.LogUtils;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;
import com.lsw.view.MyGridView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by lsw on 2018/7/11.
 */

public class OrderFillingActivity extends TitleBarActivity<OrderFillingPresenter> implements OrderFillingContract.View, View.OnClickListener, AdapterRefresh {
    DecimalFormat format = new DecimalFormat("##0.00");
    //更多方案
    @BindView(R.id.tv_more)
    TextView tv_more;
    @BindView(R.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R.id.tv_next)
    TextView tv_next;
    @BindView(R.id.ll_ishttp)
    LinearLayout ll_ishttp;
    @BindView(R.id.tv_ishttp)
    TextView tv_ishttp;
    @BindView(R.id.et_price)
    EditText et_price;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.ll_qishu)
    LinearLayout ll_qishu;
    @BindView(R.id.tv_qishu)
    TextView tv_qishu;
    @BindView(R.id.ll_fnegqigongsi)
    LinearLayout ll_fnegqigongsi;
    @BindView(R.id.tv_fenqigongsi)
    TextView tv_fenqigongsi;
    @BindView(R.id.gridview)
    MyGridView gridview;
    FujiaxianAdapter fujiaxianAdapter;
    @BindView(R.id.gridview2)
    MyGridView gridview2;
    CalculationPriceAdapter calculationPriceAdapter;
    @BindView(R.id.ll_fenqichanping)
    LinearLayout ll_fenqichanping;
    @BindView(R.id.tv_fenqichanping)
    TextView tv_fenqichanping;
    @BindView(R.id.et_shoufu)
    TextView et_shoufu;
    @BindView(R.id.tv_brand_name)
    TextView tv_brand_name;
    @BindView(R.id.tv_phone_name)
    TextView tv_phone_name;
    //直降
    @BindView(R.id.tv_zhijiang)
    TextView tv_zhijiang;
    @BindView(R.id.et_price_zhijiang)
    EditText et_price_zhijiang;
    //贷款本金
    @BindView(R.id.tv_benjin)
    TextView tv_benjin;
    @BindView(R.id.tv_price_benjin)
    TextView tv_price_benjin;
    //购机方式
    @BindView(R.id.gridview4)
    GridView gridview4;
    PurchaseModeAdapter purchaseModeAdapter;
    // 购机方式，1裸机2直降3合约
    int phone_type = 1;
    //合约机套餐ID
    String package_id;
    //非合约机
    @BindView(R.id.ll_feiheyueji)
    LinearLayout ll_feiheyueji;
    //合约机
    @BindView(R.id.ll_heyueji)
    LinearLayout ll_heyueji;
    //运营商
    @BindView(R.id.ll_yunyinshang)
    LinearLayout ll_yunyinshang;
    @BindView(R.id.tv_yunyinshang)
    TextView tv_yunyinshang;
    //选择套餐
    @BindView(R.id.ll_taocan)
    LinearLayout ll_taocan;
    @BindView(R.id.tv_taocan)
    TextView tv_taocan;
    //资费档
    @BindView(R.id.ll_zifeidang)
    LinearLayout ll_zifeidang;
    @BindView(R.id.tv_zifeidang)
    TextView tv_zifeidang;
    //合约期限
    @BindView(R.id.ll_heyueqixian)
    LinearLayout ll_heyueqixian;
    @BindView(R.id.tv_heyueqixian)
    TextView tv_heyueqixian;
    //返费比例
    @BindView(R.id.tv_fanfeibilie)
    TextView tv_fanfeibilie;
    float fanfeibilie;
    //返费总额
    @BindView(R.id.tv_fanfeizonge)
    TextView tv_fanfeizonge;
    //每月返费
    @BindView(R.id.tv_meiyuefanfei)
    TextView tv_meiyuefanfei;
    //销售价格
    @BindView(R.id.tv_xiaoshoujiage)
    TextView tv_xiaoshoujiage;
    //是否合约机,1是2否
    String is_contract;


    String memory, color, brand_name, name, price, number, id, periods = "6";
    String fenqigongsi_id, fenqichanping_id, shoufu, is_new = "1";
    double minshoufu = 0, shoufurate, sprice, zhijiang;
    private Gson gson = new Gson();
    //更多方案
    MoreOptionsModel moreOptionsModel;

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    /**
     * 添加运营商数据
     */
    public void inityunyinshang() {
        PopupWindowSortModel model1 = new PopupWindowSortModel();
        model1.setName("中国移动");
        model1.setId("1");
        list_yunyinshang.add(model1);
        PopupWindowSortModel model2 = new PopupWindowSortModel();
        model2.setName("中国联通");
        model2.setId("2");
        list_yunyinshang.add(model2);
        PopupWindowSortModel model3 = new PopupWindowSortModel();
        model3.setName("中国电信");
        model3.setId("3");
        list_yunyinshang.add(model3);
    }

    /**
     * 添加是否入网数据
     */
    public void initruwang() {
        PopupWindowSortModel model1 = new PopupWindowSortModel();
        model1.setName("是");
        model1.setId("1");
        list1.add(model1);
        PopupWindowSortModel model2 = new PopupWindowSortModel();
        model2.setName("否");
        model2.setId("2");
        list1.add(model2);
    }

    /**
     * 添加分期期数数据
     */
    public void initfenqiqishu() {
        PopupWindowSortModel model3 = new PopupWindowSortModel();
        model3.setName("6");
        model3.setId("3");
        list_qishu.add(model3);
        PopupWindowSortModel model4 = new PopupWindowSortModel();
        model4.setName("12");
        model4.setId("4");
        list_qishu.add(model4);
        PopupWindowSortModel model5 = new PopupWindowSortModel();
        model5.setName("24");
        model5.setId("5");
        list_qishu.add(model5);
    }

    @Override
    protected void initEventAndData() {
        is_contract = getIntent().getStringExtra("is_contract");
        memory = getIntent().getStringExtra("memory");
        color = getIntent().getStringExtra("color");
        brand_name = getIntent().getStringExtra("brand_name");
        name = getIntent().getStringExtra("name");
        price = getIntent().getStringExtra("price");
        number = getIntent().getStringExtra("number");
        id = getIntent().getStringExtra("id");
        tv_brand_name.setText(brand_name);
        tv_phone_name.setText(name);
        if (!TextUtils.equals("1", is_contract)) {
            sprice = Float.valueOf(price) * Float.valueOf(number);
            et_price.setText(sprice + "");
        } else {
            sprice = 0;
            et_price.setText("");
        }
        tv_more.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        tv_next.setOnClickListener(this);
        ll_ishttp.setOnClickListener(this);
        ll_qishu.setOnClickListener(this);
        ll_fnegqigongsi.setOnClickListener(this);
        ll_fenqichanping.setOnClickListener(this);
        ll_yunyinshang.setOnClickListener(this);
        ll_taocan.setOnClickListener(this);
        ll_zifeidang.setOnClickListener(this);
        ll_heyueqixian.setOnClickListener(this);
        initruwang();
        initfenqiqishu();
        inityunyinshang();
        mPresenter.getFujiaxian(periods);
        fujiaxianAdapter = new FujiaxianAdapter(mContext);
        fujiaxianAdapter.setAdapterRefresh(this);
        gridview.setAdapter(fujiaxianAdapter);
        calculationPriceAdapter = new CalculationPriceAdapter(mContext);
        gridview2.setAdapter(calculationPriceAdapter);
        List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
        CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
        calculationPriceModel.setName("贷款总额：");
        calculationPriceModel.setPrice("0");
        calculationPriceModels.add(calculationPriceModel);
        CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
        calculationPriceModel1.setName("月供金额：");
        calculationPriceModel1.setPrice("0");
        calculationPriceModels.add(calculationPriceModel1);
        calculationPriceAdapter.setData(calculationPriceModels);

        purchaseModeAdapter = new PurchaseModeAdapter(mContext);
        List<String> list = new ArrayList<>();
        if (TextUtils.equals("1", is_contract)) {
            phone_type = 3;
            ll_feiheyueji.setVisibility(View.GONE);
            ll_heyueji.setVisibility(View.VISIBLE);
            tv_taocan.setText("");
            list_taocan.clear();
            tv_zifeidang.setText("");
            list_zifeidang.clear();
            tv_fanfeibilie.setText("");
            tv_heyueqixian.setText("");
            tv_fanfeizonge.setText("");
            tv_meiyuefanfei.setText("");
            tv_xiaoshoujiage.setText("");
            sprice = 0;
            jisuan();
            list.add("合约");
        } else {
            ll_feiheyueji.setVisibility(View.VISIBLE);
            ll_heyueji.setVisibility(View.GONE);
            phone_type = 1;
            tv_benjin.setTextColor(mContext.getResources().getColor(R.color.color_c7c7c7));
            tv_zhijiang.setTextColor(mContext.getResources().getColor(R.color.color_c7c7c7));
            et_price_zhijiang.setFocusable(false);
            et_price_zhijiang.setText("");
            tv_price_benjin.setText("");
            list.add("裸机");
            list.add("直降");
        }
        gridview4.setAdapter(purchaseModeAdapter);
        purchaseModeAdapter.setData(list);
        et_price_zhijiang.setFocusable(false);
        gridview4.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (purchaseModeAdapter.getIsseclect() == position)
                    return;
                purchaseModeAdapter.setIsseclect(position);

                switch (purchaseModeAdapter.getList().get(position)) {
                    case "裸机":
                        ll_feiheyueji.setVisibility(View.VISIBLE);
                        ll_heyueji.setVisibility(View.GONE);
                        phone_type = 1;
                        tv_benjin.setTextColor(mContext.getResources().getColor(R.color.color_c7c7c7));
                        tv_zhijiang.setTextColor(mContext.getResources().getColor(R.color.color_c7c7c7));
                        et_price_zhijiang.setFocusable(false);
                        et_price_zhijiang.setText("");
                        tv_price_benjin.setText("");
                        String ps = et_price.getText().toString();
                        if (!StringUtil.isNullOrEmpty(ps))
                            et_price.setText(ps);
                        else
                            et_price.setText("");
                        break;
                    case "直降":
                        ll_feiheyueji.setVisibility(View.VISIBLE);
                        ll_heyueji.setVisibility(View.GONE);
                        phone_type = 2;
                        tv_benjin.setTextColor(mContext.getResources().getColor(R.color.color_666666));
                        tv_zhijiang.setTextColor(mContext.getResources().getColor(R.color.color_666666));
                        et_price_zhijiang.setFocusable(true);
                        et_price_zhijiang.setFocusableInTouchMode(true);
                        et_price_zhijiang.requestFocus();
                        String pss = et_price.getText().toString();
                        if (!StringUtil.isNullOrEmpty(pss))
                            et_price.setText(pss);
                        else
                            et_price.setText("");
                        break;
                    case "合约":
                        et_price.setText("");
                        phone_type = 3;
                        ll_feiheyueji.setVisibility(View.GONE);
                        ll_heyueji.setVisibility(View.VISIBLE);
                        tv_taocan.setText("");
                        list_taocan.clear();
                        tv_zifeidang.setText("");
                        list_zifeidang.clear();
                        tv_fanfeibilie.setText("");
                        tv_heyueqixian.setText("");
                        tv_fanfeizonge.setText("");
                        tv_meiyuefanfei.setText("");
                        tv_xiaoshoujiage.setText("");
                        sprice = 0;
                        jisuan();
                        break;


                }
            }
        });
        et_price_zhijiang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!StringUtil.isNullOrEmpty(s.toString())) {
                    zhijiang = Double.valueOf(s.toString());
                } else {
                    zhijiang = 0;
                }
                String zp = et_price.getText().toString().trim();
                if (!StringUtil.isNullOrEmpty(zp)) {
                    sprice = Double.valueOf(zp);
                } else {
                    sprice = 0;
                }
                if (zhijiang > sprice) {
                    et_price_zhijiang.setText(format.format(sprice));
                    tv_price_benjin.setText("0");
                    return;
                } else {
                    sprice = sprice - zhijiang;
                    tv_price_benjin.setText(format.format(sprice));
                }


                price = format.format(sprice / Integer.valueOf(number));
                et_shoufu.setText(format.format(sprice * shoufurate));
                shoufu = et_shoufu.getText().toString().trim();
                if (StringUtil.isNullOrEmpty(shoufu)) {
                    shoufu = "0";
                } else {
                    shoufu = et_shoufu.getText().toString().trim();
                }
                if (Float.valueOf(shoufu) > sprice) {
                    et_shoufu.setText(sprice + "");
                    shoufu = sprice + "";
                }
                LogUtils.i("lsw", "price==" + price);
                LogUtils.i("lsw", "shoufu==" + shoufu);
                if (StringUtil.isNullOrEmpty(fenqichanping_id))
                    return;
                if (ismore) {
                    if (moreOptionsModel != null) {
                        double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
                        List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                        for (int j = 0; j < moreOptionsModel.getInsurance_list().size(); j++) {
                            switch (moreOptionsModel.getInsurance_list().get(j).getId()) {
                                case "1":
                                    price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                                    CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                                    calculationPriceModel2.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel2.setPrice(format.format(price1));
                                    calculationPriceModels.add(calculationPriceModel2);
                                    LogUtils.i("lsw", "price1==" + price1);
                                    break;
                                case "2":
                                    price2 = Float.valueOf(price) * Float.valueOf(number) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100);
                                    CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                                    calculationPriceModel3.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel3.setPrice(format.format(price2));
                                    calculationPriceModels.add(calculationPriceModel3);
                                    LogUtils.i("lsw", "price2==" + price2);
                                    break;
                                case "3":
                                    price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate());
                                    CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                                    calculationPriceModel4.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel4.setPrice(format.format(price3));
                                    calculationPriceModels.add(calculationPriceModel4);
                                    LogUtils.i("lsw", "price3==" + price3);
                                    break;
                                case "4":
                                    price4 = Float.valueOf(price) * Integer.valueOf(number) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100);
                                    CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                                    calculationPriceModel5.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel5.setPrice(format.format(price4));
                                    calculationPriceModels.add(calculationPriceModel5);
                                    LogUtils.i("lsw", "price4==" + price4);
                                    break;
                            }
                        }

                        CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                        calculationPriceModel.setName("贷款总额：");
                        double zonge = price1 + price2 + price3 + price4;
                        calculationPriceModel.setPrice(format.format(zonge));
                        calculationPriceModels.add(0, calculationPriceModel);
                        CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                        calculationPriceModel1.setName("月供金额：");
                        double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
                        calculationPriceModel1.setPrice(format.format(yuegong));
                        calculationPriceModels.add(1, calculationPriceModel1);
                        calculationPriceAdapter.setData(calculationPriceModels);

                    }
                    return;
                }
                double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
                List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                for (int j = 0; j < fenqichanpingModels.get(fujixian_position).getInsurance_list().size(); j++) {
                    switch (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getInsurance_id()) {
                        case 1:
                            price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                            CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                            calculationPriceModel2.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel2.setPrice(format.format(price1));
                            calculationPriceModels.add(calculationPriceModel2);
                            LogUtils.i("lsw", "price1==" + price1);
                            break;
                        case 2:
                            price2 = Float.valueOf(price) * Float.valueOf(number) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100);
                            CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                            calculationPriceModel3.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel3.setPrice(format.format(price2));
                            calculationPriceModels.add(calculationPriceModel3);
                            LogUtils.i("lsw", "price2==" + price2);
                            break;
                        case 3:
                            price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate();
                            CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                            calculationPriceModel4.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel4.setPrice(format.format(price3));
                            calculationPriceModels.add(calculationPriceModel4);
                            LogUtils.i("lsw", "price3==" + price3);
                            break;
                        case 4:
                            price4 = Float.valueOf(price) * Integer.valueOf(number) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100);
                            CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                            calculationPriceModel5.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel5.setPrice(format.format(price4));
                            calculationPriceModels.add(calculationPriceModel5);
                            LogUtils.i("lsw", "price4==" + price4);
                            break;
                    }
                }

                CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                calculationPriceModel.setName("贷款总额：");
                double zonge = price1 + price2 + price3 + price4;
                calculationPriceModel.setPrice(format.format(zonge));
                calculationPriceModels.add(0, calculationPriceModel);
                CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                calculationPriceModel1.setName("月供金额：");
                double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
                calculationPriceModel1.setPrice(format.format(yuegong));
                calculationPriceModels.add(1, calculationPriceModel1);
                calculationPriceAdapter.setData(calculationPriceModels);

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_price.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isNullOrEmpty(s.toString())) {
                    sprice = 0;
                } else {
                    sprice = Double.valueOf(s.toString());
                }

//计算本金
                if (phone_type == 2) {
                    String p1 = et_price_zhijiang.getText().toString();
                    double p2;
                    if (!StringUtil.isNullOrEmpty(p1)) {
                        p2 = Double.valueOf(p1);
                        if (p2 > sprice) {
                            et_price_zhijiang.setText(format.format(sprice));
                            tv_price_benjin.setText("0");
                        } else {
                            tv_price_benjin.setText(format.format(sprice - p2));
                        }
                        sprice = sprice - p2;
                    } else {
                        if (sprice == 0) {
                            tv_price_benjin.setText("");
                        } else
                            tv_price_benjin.setText(format.format(sprice));
                    }
                }


                price = format.format(sprice / Integer.valueOf(number));
                et_shoufu.setText(format.format(sprice * shoufurate));
                shoufu = et_shoufu.getText().toString().trim();
                if (StringUtil.isNullOrEmpty(shoufu)) {
                    shoufu = "0";
                } else {
                    shoufu = et_shoufu.getText().toString().trim();
                }
                if (Float.valueOf(shoufu) > sprice) {
                    et_shoufu.setText(sprice + "");
                    shoufu = sprice + "";
                }
                LogUtils.i("lsw", "price==" + price);
                LogUtils.i("lsw", "shoufu==" + shoufu);
                if (StringUtil.isNullOrEmpty(fenqichanping_id))
                    return;
                if (ismore) {
                    if (moreOptionsModel != null) {
                        double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
                        List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                        for (int j = 0; j < moreOptionsModel.getInsurance_list().size(); j++) {
                            switch (moreOptionsModel.getInsurance_list().get(j).getId()) {
                                case "1":
                                    price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                                    CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                                    calculationPriceModel2.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel2.setPrice(format.format(price1));
                                    calculationPriceModels.add(calculationPriceModel2);
                                    LogUtils.i("lsw", "price1==" + price1);
                                    break;
                                case "2":
                                    price2 = Float.valueOf(price) * Float.valueOf(number) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100);
                                    CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                                    calculationPriceModel3.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel3.setPrice(format.format(price2));
                                    calculationPriceModels.add(calculationPriceModel3);
                                    LogUtils.i("lsw", "price2==" + price2);
                                    break;
                                case "3":
                                    price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate());
                                    CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                                    calculationPriceModel4.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel4.setPrice(format.format(price3));
                                    calculationPriceModels.add(calculationPriceModel4);
                                    LogUtils.i("lsw", "price3==" + price3);
                                    break;
                                case "4":
                                    price4 = Float.valueOf(price) * Integer.valueOf(number) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100);
                                    CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                                    calculationPriceModel5.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel5.setPrice(format.format(price4));
                                    calculationPriceModels.add(calculationPriceModel5);
                                    LogUtils.i("lsw", "price4==" + price4);
                                    break;
                            }
                        }

                        CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                        calculationPriceModel.setName("贷款总额：");
                        double zonge = price1 + price2 + price3 + price4;
                        calculationPriceModel.setPrice(format.format(zonge));
                        calculationPriceModels.add(0, calculationPriceModel);
                        CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                        calculationPriceModel1.setName("月供金额：");
                        double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
                        calculationPriceModel1.setPrice(format.format(yuegong));
                        calculationPriceModels.add(1, calculationPriceModel1);
                        calculationPriceAdapter.setData(calculationPriceModels);

                    }
                    return;
                }
                double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
                List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                for (int j = 0; j < fenqichanpingModels.get(fujixian_position).getInsurance_list().size(); j++) {
                    switch (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getInsurance_id()) {
                        case 1:
                            price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                            CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                            calculationPriceModel2.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel2.setPrice(format.format(price1));
                            calculationPriceModels.add(calculationPriceModel2);
                            LogUtils.i("lsw", "price1==" + price1);
                            break;
                        case 2:
                            price2 = Float.valueOf(price) * Float.valueOf(number) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100);
                            CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                            calculationPriceModel3.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel3.setPrice(format.format(price2));
                            calculationPriceModels.add(calculationPriceModel3);
                            LogUtils.i("lsw", "price2==" + price2);
                            break;
                        case 3:
                            price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate();
                            CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                            calculationPriceModel4.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel4.setPrice(format.format(price3));
                            calculationPriceModels.add(calculationPriceModel4);
                            LogUtils.i("lsw", "price3==" + price3);
                            break;
                        case 4:
                            price4 = Float.valueOf(price) * Integer.valueOf(number) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100);
                            CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                            calculationPriceModel5.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel5.setPrice(format.format(price4));
                            calculationPriceModels.add(calculationPriceModel5);
                            LogUtils.i("lsw", "price4==" + price4);
                            break;
                    }
                }

                CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                calculationPriceModel.setName("贷款总额：");
                double zonge = price1 + price2 + price3 + price4;
                calculationPriceModel.setPrice(format.format(zonge));
                calculationPriceModels.add(0, calculationPriceModel);
                CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                calculationPriceModel1.setName("月供金额：");
                double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
                calculationPriceModel1.setPrice(format.format(yuegong));
                calculationPriceModels.add(1, calculationPriceModel1);
                calculationPriceAdapter.setData(calculationPriceModels);

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_shoufu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isNullOrEmpty(s.toString())) {
                    shoufu = "0";
                } else {
                    shoufu = s.toString();
                }
                if (Float.valueOf(shoufu) > sprice) {
                    et_shoufu.setText(sprice + "");
                    shoufu = sprice + "";
                }
                if (StringUtil.isNullOrEmpty(fenqichanping_id))
                    return;
                if (ismore) {
                    if (moreOptionsModel != null) {
                        double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
                        List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                        for (int j = 0; j < moreOptionsModel.getInsurance_list().size(); j++) {
                            switch (moreOptionsModel.getInsurance_list().get(j).getId()) {
                                case "1":
                                    price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                                    CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                                    calculationPriceModel2.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel2.setPrice(format.format(price1));
                                    calculationPriceModels.add(calculationPriceModel2);
                                    break;
                                case "2":
                                    price2 = Float.valueOf(price) * Float.valueOf(number) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100);
                                    CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                                    calculationPriceModel3.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel3.setPrice(format.format(price2));
                                    calculationPriceModels.add(calculationPriceModel3);
                                    break;
                                case "3":
                                    price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate());
                                    CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                                    calculationPriceModel4.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel4.setPrice(format.format(price3));
                                    calculationPriceModels.add(calculationPriceModel4);
                                    break;
                                case "4":
                                    price4 = Float.valueOf(price) * Integer.valueOf(number) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100);
                                    CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                                    calculationPriceModel5.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                                    calculationPriceModel5.setPrice(format.format(price4));
                                    calculationPriceModels.add(calculationPriceModel5);
                                    break;
                            }
                        }

                        CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                        calculationPriceModel.setName("贷款总额：");
                        double zonge = price1 + price2 + price3 + price4;
                        calculationPriceModel.setPrice(format.format(zonge));
                        calculationPriceModels.add(0, calculationPriceModel);
                        CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                        calculationPriceModel1.setName("月供金额：");
                        double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
                        calculationPriceModel1.setPrice(format.format(yuegong));
                        calculationPriceModels.add(1, calculationPriceModel1);
                        calculationPriceAdapter.setData(calculationPriceModels);

                    }
                    return;
                }
                double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
                List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                for (int j = 0; j < fenqichanpingModels.get(fujixian_position).getInsurance_list().size(); j++) {
                    switch (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getInsurance_id()) {
                        case 1:
                            price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                            CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                            calculationPriceModel2.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel2.setPrice(format.format(price1));
                            calculationPriceModels.add(calculationPriceModel2);
                            break;
                        case 2:
                            price2 = Float.valueOf(price) * Float.valueOf(number) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100);
                            CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                            calculationPriceModel3.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel3.setPrice(format.format(price2));
                            calculationPriceModels.add(calculationPriceModel3);
                            break;
                        case 3:
                            price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate();
                            CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                            calculationPriceModel4.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel4.setPrice(format.format(price3));
                            calculationPriceModels.add(calculationPriceModel4);
                            break;
                        case 4:
                            price4 = Float.valueOf(price) * Integer.valueOf(number) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100);
                            CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                            calculationPriceModel5.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel5.setPrice(format.format(price4));
                            calculationPriceModels.add(calculationPriceModel5);
                            break;
                    }
                }

                CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                calculationPriceModel.setName("贷款总额：");
                double zonge = price1 + price2 + price3 + price4;
                calculationPriceModel.setPrice(format.format(zonge));
                calculationPriceModels.add(0, calculationPriceModel);
                CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                calculationPriceModel1.setName("月供金额：");
                double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
                calculationPriceModel1.setPrice(format.format(yuegong));
                calculationPriceModels.add(1, calculationPriceModel1);
                calculationPriceAdapter.setData(calculationPriceModels);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected int setContentLayoutView() {
        return R.layout.activity_order_filling;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_more:
                shoufu = et_shoufu.getText().toString().trim();
                if (StringUtil.isNullOrEmpty(shoufu))
                    shoufu = "0";
                List<String> list2 = new ArrayList<>();
                for (int i = 0; i < fujiaxianAdapter.getList().size(); i++) {
                    LogUtils.i("lsw", "isseclect==" + i + fujiaxianAdapter.getList().get(i).isIsseclect());
                    if (fujiaxianAdapter.getList().get(i).isIsseclect())
                        list2.add(fujiaxianAdapter.getList().get(i).getId() + "");
                }
                Intent intent = new Intent(mContext, MoreOptionsActivity.class);
                intent.putExtra("price", price);
                intent.putExtra("first_money", shoufu);
                intent.putExtra("insurance_id", gson.toJson(list2));
                intent.putExtra("num", number);
                intent.putExtra("periods", periods);
                startActivityForResult(intent, 1);
                break;
            case R.id.tv_next:
                String mobile = et_phone.getText().toString().trim();
                shoufu = et_shoufu.getText().toString().trim();
                if (StringUtil.isNullOrEmpty(shoufu))
                    shoufu = "0";
                if (StringUtil.isNullOrEmpty(mobile)) {
                    ToastUtil.showToast(mContext, "请输入手机号！");
                    return;
                }
                if (!StringUtil.checkPhoneNum(mobile)) {
                    ToastUtil.showToast(mContext, "请输入正确的手机号！");
                    return;
                }
                if (phone_type == 3) {
                    if (StringUtil.isNullOrEmpty(tv_yunyinshang.getText().toString().trim())) {
                        ToastUtil.showToast(mContext, "请选择运营商！");
                        return;
                    }
                    if (StringUtil.isNullOrEmpty(tv_taocan.getText().toString().trim())) {
                        ToastUtil.showToast(mContext, "请选择套餐！");
                        return;
                    }
                    if (StringUtil.isNullOrEmpty(tv_zifeidang.getText().toString().trim())) {
                        ToastUtil.showToast(mContext, "请选择资费档！");
                        return;
                    }
//                    if (StringUtil.isNullOrEmpty(tv_xiaoshoujiage.getText().toString().trim())) {
//                        ToastUtil.showToast(mContext, "请输入商品价格！");
//                        return;
//                    }
                } else if (StringUtil.isNullOrEmpty(et_price.getText().toString().trim())) {
                    ToastUtil.showToast(mContext, "请输入商品价格！");
                    return;
                }
                if (phone_type == 2)
                    if (StringUtil.isNullOrEmpty(et_price_zhijiang.getText().toString().trim())) {
                        ToastUtil.showToast(mContext, "请输入直降价格！");
                        return;
                    }
                if (StringUtil.isNullOrEmpty(fenqigongsi_id)) {
                    ToastUtil.showToast(mContext, "请选择分期公司！");
                    return;
                }
                if (StringUtil.isNullOrEmpty(fenqichanping_id)) {
                    ToastUtil.showToast(mContext, "请选择分期产品！");
                    return;
                }
                minshoufu = sprice * shoufurate;
                minshoufu = Double.valueOf(format.format(minshoufu));
                LogUtils.i("lsw", "minshoufu=" + minshoufu + ",shoufu=" + shoufu);
                if (Double.valueOf(shoufu) < minshoufu) {
                    ToastUtil.showToast(mContext, "首付金额至少" + format.format(minshoufu) + "!");
                    return;
                }

                Intent intent1 = new Intent(mContext, CustomerInformationActivity.class);
                intent1.putExtra("mobile", mobile);
                intent1.putExtra("phone_id", id + "");
                intent1.putExtra("memory", memory);
                intent1.putExtra("color", color);
                intent1.putExtra("num", number);
                intent1.putExtra("is_new", is_new);
                if (phone_type == 3) {
                    intent1.putExtra("package", package_id);
                } else
                    intent1.putExtra("package", "0");
                intent1.putExtra("price", price);
                intent1.putExtra("periods", tv_qishu.getText().toString().trim());
                intent1.putExtra("buy_type", phone_type + "");
                if (phone_type == 2) {
                    intent1.putExtra("discount_price", et_price_zhijiang.getText().toString());
                }
                if (phone_type == 3) {
                    intent1.putExtra("package_expenses", tv_zifeidang.getText().toString());
                }
                List<String> list3 = new ArrayList<>();
                for (int i = 0; i < fujiaxianAdapter.getList().size(); i++) {
                    LogUtils.i("lsw", "isseclect==" + i + fujiaxianAdapter.getList().get(i).isIsseclect());
                    if (fujiaxianAdapter.getList().get(i).isIsseclect())
                        list3.add(fujiaxianAdapter.getList().get(i).getId() + "");
                }
                intent1.putExtra("insurance_id", gson.toJson(list3));
                intent1.putExtra("company_id", fenqigongsi_id);
                intent1.putExtra("company_product_id", fenqichanping_id);
                intent1.putExtra("first_money", shoufu);
                startActivity(intent1);
                break;
            case R.id.ll_ishttp:
                initPopuptWindow(list1, tv_ishttp, 1);
                break;
            case R.id.ll_qishu:
                initPopuptWindow(list_qishu, tv_qishu, 2);
                break;
            case R.id.ll_fnegqigongsi:
                if (StringUtil.isNullOrEmpty(tv_qishu.getText().toString())) {
                    ToastUtil.showToast(mContext, "请选择分期期数！");
                    return;
                }
                List<String> list = new ArrayList<>();
                for (int i = 0; i < fujiaxianAdapter.getList().size(); i++) {
                    LogUtils.i("lsw", "isseclect==" + i + fujiaxianAdapter.getList().get(i).isIsseclect());
                    if (fujiaxianAdapter.getList().get(i).isIsseclect())
                        list.add(fujiaxianAdapter.getList().get(i).getId() + "");
                }
                mPresenter.getselectcompany(tv_qishu.getText().toString(), gson.toJson(list));
                break;
            case R.id.ll_fenqichanping:
                if (StringUtil.isNullOrEmpty(fenqigongsi_id)) {
                    ToastUtil.showToast(mContext, "请选择分期公司！");
                    return;
                }
                List<String> list1 = new ArrayList<>();
                for (int i = 0; i < fujiaxianAdapter.getList().size(); i++) {
                    LogUtils.i("lsw", "isseclect==" + i + fujiaxianAdapter.getList().get(i).isIsseclect());
                    if (fujiaxianAdapter.getList().get(i).isIsseclect())
                        list1.add(fujiaxianAdapter.getList().get(i).getId() + "");
                }
                mPresenter.getselectproduct(tv_qishu.getText().toString(), gson.toJson(list1), fenqigongsi_id);
                break;
            case R.id.ll_yunyinshang:
                initPopuptWindow(list_yunyinshang, tv_yunyinshang, 5);
                break;
            case R.id.ll_taocan:
                if (list_taocan.size() > 0) {
                    initPopuptWindow(list_taocan, tv_taocan, 6);
                } else {
                    ToastUtil.showToast(mContext, "请先选择运营商！");
                }
                break;
            case R.id.ll_zifeidang:
                if (list_zifeidang.size() > 0) {
                    initPopuptWindow(list_zifeidang, tv_zifeidang, 7);
                } else {
                    ToastUtil.showToast(mContext, "请先选择套餐！");
                }
                break;
            case R.id.ll_heyueqixian:

                break;

        }
    }

    @Override
    public void setData() {

    }

    List<ContractPhoneModel> list_heyuejitaocan = new ArrayList<>();

    @Override
    public void setHeyuejiTaocan(List<ContractPhoneModel> list) {
        if (list != null) {
            list_heyuejitaocan = list;
            list_taocan.clear();
            for (int i = 0; i < list.size(); i++) {
                PopupWindowSortModel model = new PopupWindowSortModel();
                model.setName(list.get(i).getName());
                model.setId(list.get(i).getId() + "");
                list_taocan.add(model);
            }
        }
    }

    @Override
    public void setselectcompany(List<FenqigongsiModel> list) {
        if (list == null)
            list = new ArrayList<>();
        List<PopupWindowSortModel> models = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            PopupWindowSortModel model = new PopupWindowSortModel();
            model.setName(list.get(i).getName());
            model.setId(list.get(i).getId() + "");
            models.add(model);
        }
        if (models.size() > 0) {
            initPopuptWindow(models, tv_fenqigongsi, 3);
        } else {
            ToastUtil.showToast(mContext, "没有符合条件的公司！");
        }
    }

    int fujixian_position;
    List<FenqichanpingModel> fenqichanpingModels = new ArrayList<>();

    @Override
    public void setselectproduct(List<FenqichanpingModel> list) {
        fenqichanpingModels = list;
        if (list == null)
            list = new ArrayList<>();
        List<PopupWindowSortModel> models = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            PopupWindowSortModel model = new PopupWindowSortModel();
            model.setName(list.get(i).getCompany_product_name());
            model.setId(list.get(i).getCompany_product_id() + "");
            models.add(model);
        }
        if (models.size() > 0) {
            initPopuptWindow(models, tv_fenqichanping, 4);
        } else {
            ToastUtil.showToast(mContext, "该公司没有符合条件的产品！");
        }
    }

    @Override
    public void setFujiaxian(List<FujiaxianModel> list) {
        fujiaxianAdapter.setData(list);
    }

    @Override
    public void onRefreshAdapter(int position, int taskid) {
        tv_fenqigongsi.setText("");
        tv_fenqichanping.setText("");
        fenqichanping_id = "";
        fenqigongsi_id = "";
        ismore = false;
        List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
        CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
        calculationPriceModel.setName("贷款总额：");
        calculationPriceModel.setPrice("0");
        calculationPriceModels.add(calculationPriceModel);
        CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
        calculationPriceModel1.setName("月供金额：");
        calculationPriceModel1.setPrice("0");
        calculationPriceModels.add(calculationPriceModel1);
        calculationPriceAdapter.setData(calculationPriceModels);
    }

    @Override
    protected void setActionBarDetail() {

    }

    boolean ismore = false;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1:
                    moreOptionsModel = (MoreOptionsModel) data.getSerializableExtra("data");
                    if (moreOptionsModel != null) {
                        ismore = true;
                        tv_qishu.setText(moreOptionsModel.getPeriods() + "");
                        tv_fenqigongsi.setText(moreOptionsModel.getCompany_name());
                        fenqigongsi_id = moreOptionsModel.getCompany_id() + "";
                        tv_fenqichanping.setText(moreOptionsModel.getCompany_product_name());
                        fenqichanping_id = moreOptionsModel.getCompany_product_id() + "";
                        minshoufu = Double.valueOf(moreOptionsModel.getFirst_money());
                        shoufurate = Double.valueOf(moreOptionsModel.getMin_money()) / 100;
                        et_shoufu.setText(format.format(minshoufu));
                    }
                    break;
            }
        }
    }

    //贷款本金= 商品价格-首付
    //贷款总额：=（贷款本金*利率）+贷款本金
    //月供金额=贷款总额/分期期数

    PopupWindow popupWindow;
    private List<PopupWindowSortModel> list1 = new ArrayList<>();
    private List<PopupWindowSortModel> list_qishu = new ArrayList<>();
    private List<PopupWindowSortModel> list_yunyinshang = new ArrayList<>();
    private List<PopupWindowSortModel> list_taocan = new ArrayList<>();
    private List<PopupWindowSortModel> list_zifeidang = new ArrayList<>();
    private List<PopupWindowSortModel> list_heyueqixian = new ArrayList<>();
    PopupWindowSortAdapter popupWindowSortAdapter;
    View popupWindow_view;
    ListView listView;

    /**
     * 创建PopupWindow
     */
    protected void initPopuptWindow(List<PopupWindowSortModel> list1, View view_bottom, int ispos) {
        // TODO Auto-generated method stub
        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        popupWindow_view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_sort, null,
                false);
        listView = popupWindow_view.findViewById(R.id.listview);
        popupWindowSortAdapter = new PopupWindowSortAdapter(mContext);
        popupWindowSortAdapter.setData(list1);
        listView.setAdapter(popupWindowSortAdapter);
        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            PopupWindowSortModel item = popupWindowSortAdapter.getItem(i);
            if (ispos == 1) {
                tv_ishttp.setText(item.getName());
                is_new = item.getId();
            } else if (ispos == 2) {
                tv_qishu.setText(item.getName());
                tv_fenqigongsi.setText("");
                tv_fenqichanping.setText("");
                fenqichanping_id = "";
                fenqigongsi_id = "";
                ismore = false;
                List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                calculationPriceModel.setName("贷款总额：");
                calculationPriceModel.setPrice("0");
                calculationPriceModels.add(calculationPriceModel);
                CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                calculationPriceModel1.setName("月供金额：");
                calculationPriceModel1.setPrice("0");
                calculationPriceModels.add(calculationPriceModel1);
                calculationPriceAdapter.setData(calculationPriceModels);
            } else if (ispos == 3) {
                tv_fenqigongsi.setText(item.getName());
                fenqigongsi_id = item.getId();
                tv_fenqichanping.setText("");
                fenqichanping_id = "";
                ismore = false;
                List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                calculationPriceModel.setName("贷款总额：");
                calculationPriceModel.setPrice("0");
                calculationPriceModels.add(calculationPriceModel);
                CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                calculationPriceModel1.setName("月供金额：");
                calculationPriceModel1.setPrice("0");
                calculationPriceModels.add(calculationPriceModel1);
                calculationPriceAdapter.setData(calculationPriceModels);
            } else if (ispos == 4) {
                tv_fenqichanping.setText(item.getName());
                fenqichanping_id = item.getId();
                fujixian_position = i;
                shoufu = et_shoufu.getText().toString().trim();
                ismore = false;
                if (StringUtil.isNullOrEmpty(shoufu))
                    shoufu = "0";
                double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
                List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                for (int j = 0; j < fenqichanpingModels.get(i).getInsurance_list().size(); j++) {
                    switch (fenqichanpingModels.get(i).getInsurance_list().get(j).getInsurance_id()) {
                        case 1:
                            price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (fenqichanpingModels.get(i).getInsurance_list().get(j).getRate() / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                            CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                            calculationPriceModel2.setName(fenqichanpingModels.get(i).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel2.setPrice(format.format(price1));
                            calculationPriceModels.add(calculationPriceModel2);
                            break;
                        case 2:
                            price2 = Float.valueOf(price) * Float.valueOf(number) * (fenqichanpingModels.get(i).getInsurance_list().get(j).getRate() / 100);
                            CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                            calculationPriceModel3.setName(fenqichanpingModels.get(i).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel3.setPrice(format.format(price2));
                            calculationPriceModels.add(calculationPriceModel3);
                            break;
                        case 3:
                            price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * fenqichanpingModels.get(i).getInsurance_list().get(j).getRate();
                            CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                            calculationPriceModel4.setName(fenqichanpingModels.get(i).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel4.setPrice(format.format(price3));
                            calculationPriceModels.add(calculationPriceModel4);
                            break;
                        case 4:
                            price4 = Float.valueOf(price) * Integer.valueOf(number) * (fenqichanpingModels.get(i).getInsurance_list().get(j).getRate() / 100);
                            CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                            calculationPriceModel5.setName(fenqichanpingModels.get(i).getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel5.setPrice(format.format(price4));
                            calculationPriceModels.add(calculationPriceModel5);
                            break;
                    }
                }

                CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                calculationPriceModel.setName("贷款总额：");
                double zonge = price1 + price2 + price3 + price4;
                calculationPriceModel.setPrice(format.format(zonge));
                calculationPriceModels.add(0, calculationPriceModel);
                CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                calculationPriceModel1.setName("月供金额：");
                double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
                calculationPriceModel1.setPrice(format.format(yuegong));
                calculationPriceModels.add(1, calculationPriceModel1);
                calculationPriceAdapter.setData(calculationPriceModels);
                minshoufu = Float.valueOf(price) * Float.valueOf(number) * (Float.valueOf(fenqichanpingModels.get(i).getMin_money()) / 100);
                shoufurate = Double.valueOf(fenqichanpingModels.get(i).getMin_money()) / 100;
                et_shoufu.setText(format.format(minshoufu));
            } else if (ispos == 5) {
                //选择运营商
                tv_yunyinshang.setText(item.getName());
                mPresenter.getHeyuejiTaocan(item.getId());
                tv_taocan.setText("");
                list_taocan.clear();
                tv_zifeidang.setText("");
                list_zifeidang.clear();
                tv_fanfeibilie.setText("");
                tv_heyueqixian.setText("");
                tv_fanfeizonge.setText("");
                tv_meiyuefanfei.setText("");
                tv_xiaoshoujiage.setText("");
                sprice = 0;
                jisuan();
            } else if (ispos == 6) {
                //选择套餐
                tv_taocan.setText(item.getName());
                tv_zifeidang.setText("");
                tv_fanfeizonge.setText("");
                tv_meiyuefanfei.setText("");
                tv_xiaoshoujiage.setText("");
                if (list_heyuejitaocan != null) {
                    tv_fanfeibilie.setText(list_heyuejitaocan.get(i).getBack_rate() + "%");
                    fanfeibilie = Float.valueOf(list_heyuejitaocan.get(i).getBack_rate());
                    periods = list_heyuejitaocan.get(i).getPeriods() + "";
                    package_id = list_heyuejitaocan.get(i).getId() + "";
                    tv_qishu.setText(periods);
                    tv_heyueqixian.setText(list_heyuejitaocan.get(i).getPeriods() + "");
                    list_zifeidang.clear();
                    for (int j = 0; j < list_heyuejitaocan.get(i).getExpenses().size(); j++) {
                        PopupWindowSortModel model = new PopupWindowSortModel();
                        model.setName(list_heyuejitaocan.get(i).getExpenses().get(j));
                        list_zifeidang.add(model);
                    }
                }
                sprice = 0;
                jisuan();
            } else if (ispos == 7) {
                //选择资费档
                tv_zifeidang.setText(item.getName());
                double price = Double.valueOf(item.getName());
                double meiyuefanfei = price * fanfeibilie / 100;
                tv_meiyuefanfei.setText(format.format(meiyuefanfei));
                tv_fanfeizonge.setText(format.format(meiyuefanfei * Integer.valueOf(periods)));
                tv_xiaoshoujiage.setText(format.format(price * Integer.valueOf(periods)));
                sprice = price * Integer.valueOf(periods);
                jisuan();
            } else if (ispos == 8) {
                //选择合约期限
                tv_heyueqixian.setText(item.getName());
            }

            popupWindow.dismiss();
        });
//        }
        popupWindow = new PopupWindow(popupWindow_view, DensityUtil.getWidth(mContext) - DensityUtil.dip2px(mContext, 160), LinearLayout.LayoutParams.WRAP_CONTENT, true);
//        // 设置动画效果
//        popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 点击其他地方消失
        if (Build.VERSION.SDK_INT < 24) {
            popupWindow.showAsDropDown(view_bottom);
        } else {
            // 适配 android 7.0
            int[] location = new int[2];
            view_bottom.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            popupWindow.showAtLocation(view_bottom, Gravity.NO_GRAVITY, DensityUtil.dip2px(mContext, 120), y + view_bottom.getHeight() + DensityUtil.dip2px(mContext, 3));
        }
        popupWindow.setOnDismissListener(() -> {
            popupWindow = null;
        });
    }

    /**
     * 计算金额
     */
    public void jisuan() {
        price = format.format(sprice / Integer.valueOf(number));
        et_shoufu.setText(format.format(sprice * shoufurate));
        shoufu = et_shoufu.getText().toString().trim();
        if (StringUtil.isNullOrEmpty(shoufu)) {
            shoufu = "0";
        } else {
            shoufu = et_shoufu.getText().toString().trim();
        }
        if (Float.valueOf(shoufu) > sprice) {
            et_shoufu.setText(sprice + "");
            shoufu = sprice + "";
        }
        LogUtils.i("lsw", "price==" + price);
        LogUtils.i("lsw", "shoufu==" + shoufu);
        if (StringUtil.isNullOrEmpty(fenqichanping_id))
            return;
        if (ismore) {
            if (moreOptionsModel != null) {
                double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
                List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
                for (int j = 0; j < moreOptionsModel.getInsurance_list().size(); j++) {
                    switch (moreOptionsModel.getInsurance_list().get(j).getId()) {
                        case "1":
                            price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                            CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                            calculationPriceModel2.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel2.setPrice(format.format(price1));
                            calculationPriceModels.add(calculationPriceModel2);
                            LogUtils.i("lsw", "price1==" + price1);
                            break;
                        case "2":
                            price2 = Float.valueOf(price) * Float.valueOf(number) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100);
                            CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                            calculationPriceModel3.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel3.setPrice(format.format(price2));
                            calculationPriceModels.add(calculationPriceModel3);
                            LogUtils.i("lsw", "price2==" + price2);
                            break;
                        case "3":
                            price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate());
                            CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                            calculationPriceModel4.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel4.setPrice(format.format(price3));
                            calculationPriceModels.add(calculationPriceModel4);
                            LogUtils.i("lsw", "price3==" + price3);
                            break;
                        case "4":
                            price4 = Float.valueOf(price) * Integer.valueOf(number) * (Float.valueOf(moreOptionsModel.getInsurance_list().get(j).getRate()) / 100);
                            CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                            calculationPriceModel5.setName(moreOptionsModel.getInsurance_list().get(j).getName() + "：");
                            calculationPriceModel5.setPrice(format.format(price4));
                            calculationPriceModels.add(calculationPriceModel5);
                            LogUtils.i("lsw", "price4==" + price4);
                            break;
                    }
                }

                CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
                calculationPriceModel.setName("贷款总额：");
                double zonge = price1 + price2 + price3 + price4;
                calculationPriceModel.setPrice(format.format(zonge));
                calculationPriceModels.add(0, calculationPriceModel);
                CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
                calculationPriceModel1.setName("月供金额：");
                double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
                calculationPriceModel1.setPrice(format.format(yuegong));
                calculationPriceModels.add(1, calculationPriceModel1);
                calculationPriceAdapter.setData(calculationPriceModels);

            }
            return;
        }
        double price1 = 0, price2 = 0, price3 = 0, price4 = 0;
        List<CalculationPriceModel> calculationPriceModels = new ArrayList<>();
        for (int j = 0; j < fenqichanpingModels.get(fujixian_position).getInsurance_list().size(); j++) {
            switch (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getInsurance_id()) {
                case 1:
                    price1 = (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu)) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100) + (Float.valueOf(price) * Float.valueOf(number) - Float.valueOf(shoufu));
                    CalculationPriceModel calculationPriceModel2 = new CalculationPriceModel();
                    calculationPriceModel2.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                    calculationPriceModel2.setPrice(format.format(price1));
                    calculationPriceModels.add(calculationPriceModel2);
                    LogUtils.i("lsw", "price1==" + price1);
                    break;
                case 2:
                    price2 = Float.valueOf(price) * Float.valueOf(number) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100);
                    CalculationPriceModel calculationPriceModel3 = new CalculationPriceModel();
                    calculationPriceModel3.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                    calculationPriceModel3.setPrice(format.format(price2));
                    calculationPriceModels.add(calculationPriceModel3);
                    LogUtils.i("lsw", "price2==" + price2);
                    break;
                case 3:
                    price3 = Float.valueOf(tv_qishu.getText().toString().trim()) * fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate();
                    CalculationPriceModel calculationPriceModel4 = new CalculationPriceModel();
                    calculationPriceModel4.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                    calculationPriceModel4.setPrice(format.format(price3));
                    calculationPriceModels.add(calculationPriceModel4);
                    LogUtils.i("lsw", "price3==" + price3);
                    break;
                case 4:
                    price4 = Float.valueOf(price) * Integer.valueOf(number) * (fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getRate() / 100);
                    CalculationPriceModel calculationPriceModel5 = new CalculationPriceModel();
                    calculationPriceModel5.setName(fenqichanpingModels.get(fujixian_position).getInsurance_list().get(j).getName() + "：");
                    calculationPriceModel5.setPrice(format.format(price4));
                    calculationPriceModels.add(calculationPriceModel5);
                    LogUtils.i("lsw", "price4==" + price4);
                    break;
            }
        }

        CalculationPriceModel calculationPriceModel = new CalculationPriceModel();
        calculationPriceModel.setName("贷款总额：");
        double zonge = price1 + price2 + price3 + price4;
        calculationPriceModel.setPrice(format.format(zonge));
        calculationPriceModels.add(0, calculationPriceModel);
        CalculationPriceModel calculationPriceModel1 = new CalculationPriceModel();
        calculationPriceModel1.setName("月供金额：");
        double yuegong = zonge / Integer.valueOf(tv_qishu.getText().toString().trim());
        calculationPriceModel1.setPrice(format.format(yuegong));
        calculationPriceModels.add(1, calculationPriceModel1);
        calculationPriceAdapter.setData(calculationPriceModels);

    }

    @Override
    public void openlogin() {
        OpenLogin();
    }
}
